/**************************************************
 * Filename: LoginFrame.java
 * Version: v1.0
 * CreatedDate: 2011-11-24
 * Copyright (C) 2011 By cafebabe.
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program. If not, see http://www.gnu.org/licenses/.
 *
 * If you would like to negotiate alternate licensing terms, you may do
 * so by contacting the author: talentyao@foxmail.com
 ***************************************************/

package com.train.gui;

import java.awt.EventQueue;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JPasswordField;
import javax.swing.JTextField;
import javax.swing.border.EmptyBorder;

import org.apache.http.client.HttpClient;
import org.apache.http.impl.client.DefaultHttpClient;

import com.train.Constants;
import com.train.Result;
import com.train.TrainClient;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;

/**
 * 功能描述
 * 
 * @author cafebabe
 * @since 2011-11-24
 * @version 1.0
 */
public class LoginFrame extends JFrame {

	/**字段注释*/
	private static final long serialVersionUID = 1L;
	private JPanel contentPane;
	private JTextField txtUserName;
	private JTextField txtPassword;
	private JTextField txtRandCode;
	private JLabel lblNewLabel;
	private HttpClient httpClient = new DefaultHttpClient();

	/**
	 * Launch the application.
	 */
	public static void main(String[] args) {
		EventQueue.invokeLater(new Runnable() {
			public void run() {
				try {
					LoginFrame frame = new LoginFrame();
					frame.setVisible(true);
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
		});
	}

	/**
	 * Create the frame.
	 */
	public LoginFrame() {
		final TrainClient client = new TrainClient(httpClient);
		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		setBounds(100, 100, 360, 300);
		contentPane = new JPanel();
		contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
		setContentPane(contentPane);
		contentPane.setLayout(null);

		JLabel label = new JLabel("用户名：");
		label.setBounds(79, 57, 54, 15);
		contentPane.add(label);

		JLabel label_1 = new JLabel("密  码：");
		label_1.setBounds(79, 93, 54, 15);
		contentPane.add(label_1);

		txtUserName = new JTextField();
		txtUserName.setBounds(128, 54, 122, 21);
		contentPane.add(txtUserName);
		txtUserName.setColumns(10);
		txtUserName.setText("sg000001");

		txtPassword = new JPasswordField();
		txtPassword.setBounds(128, 90, 122, 21);
		contentPane.add(txtPassword);
		txtPassword.setColumns(10);
		txtPassword.setText("258456");

		JLabel label_2 = new JLabel("验证码：");
		label_2.setBounds(79, 131, 54, 15);
		contentPane.add(label_2);

		txtRandCode = new JTextField();
		txtRandCode.setBounds(128, 128, 81, 21);
		contentPane.add(txtRandCode);
		txtRandCode.setColumns(10);

		lblNewLabel = new JLabel(new ImageIcon(
				client.getCodeByte(Constant.LOGIN_CODE_URL)));
		lblNewLabel.addMouseListener(new MouseAdapter() {
			@Override
			public void mouseClicked(MouseEvent e) {
				lblNewLabel.setIcon(new ImageIcon(client
						.getCodeByte(Constant.LOGIN_CODE_URL)));
			}
		});
		lblNewLabel.setBounds(219, 124, 72, 25);
		contentPane.add(lblNewLabel);

		JButton btnNewButton = new JButton("登录");
		btnNewButton.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				String userName = txtUserName.getText();
				String password = txtPassword.getText();
				String randCode = txtRandCode.getText();
				if("".equals(userName) || "".equals(password) || "".equals(randCode) ) {
					JOptionPane.showMessageDialog(null, "所有信息均不能为空!");
					return;
				}
				
				Result ans = client.login(userName, password, randCode);
				if (ans.isOK()) {
					new MainFrame(httpClient);
					setVisible(false);
				} else {
					clear(client);
				}
			}
		});

		btnNewButton.setBounds(181, 193, 93, 23);
		contentPane.add(btnNewButton);

		JButton button = new JButton("取消");
		button.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				clear(client);
			}
		});
		button.setBounds(78, 193, 93, 23);
		setLocationRelativeTo(null);
		contentPane.add(button);
	}

	/**
	 * 功能描述
	 * @param lblNewLabel 
	 */
	private void clear(TrainClient client) {
		lblNewLabel.setIcon(new ImageIcon(client
				.getCodeByte(Constant.LOGIN_CODE_URL)));
		txtUserName.setText("");
		txtPassword.setText("");
		txtRandCode.setText("");
	}
}
